﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using XNAProject.Enumerations;
using System.Windows.Forms;

namespace XNAProject.GameElements
{
    [Serializable()]
    public class SaveFleet : ISerializable
    {
        public string Name { get; set; }
        public LevelSizeType size { get; set; }
        public List<SaveShip> shipCollection { get; set; }

        public SaveFleet()
        {
            shipCollection = new List<SaveShip>();
        }

        #region File

        public SaveFleet(SerializationInfo info, StreamingContext ctxt)
        {
            Name = (string)info.GetValue("Name", typeof(string));
            shipCollection = (List<SaveShip>)info.GetValue("ShipCollection", typeof(List<SaveShip>));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("Name", Name);
            info.AddValue("ShipCollection", shipCollection);
        }

        public void Save()
        {
            DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(Application.ExecutablePath) + @"\Fleets");
            if (!di.Exists)
            {
                di.Create();
            }

            Stream stream = File.Open("Fleets\\" + Name + ".m8", FileMode.Create);
            BinaryFormatter bformatter = new BinaryFormatter();

            bformatter.Serialize(stream, this);
            stream.Close();
                
        }

        public void Load(string path)
        {
            SaveFleet temp = new SaveFleet();
            Stream stream = File.Open("Fleets\\" + path, FileMode.Open);
            BinaryFormatter bformatter = new BinaryFormatter();

            temp = (SaveFleet)bformatter.Deserialize(stream);
            Name = temp.Name;
            shipCollection = temp.shipCollection;
            stream.Close();
        }

        #endregion
    }
}
